package patty.demo;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import patty.http.HttpService;

/*
 * TRequest URL:http://web.trtc.com.tw/c/2stainfo.asp
 * Request Method:POST
 * Status Code:200 OK
 * Request Headersview source
 * Content-Type:application/x-www-form-urlencoded
 * Host:web.trtc.com.tw
 * Origin:http://web.trtc.com.tw
 * Referer:http://web.trtc.com.tw/c/2stainfo.asp
 Form Dataview sourceview URL encoded
 s1elect:027
 action:query
 s2elect:023
 submit: 確定
 */
/**
 * 
 * @author Patty
 */
public class WHForeclosure {
	public static void main(String[] args) {
		Map<String, String> formData = new HashMap<String, String>();
		Document doc;
		Response res;
		try {
			// 請求WHD2A00
			Connection connection = Jsoup
					.connect("http://aomp.judicial.gov.tw/abbs/wkw/WHD2A00.jsp");
			res = connection.execute();
			// System.out.println(res.body());
			// get page title
			doc = res.parse();
			System.out.println("title : " + doc.title());

			// 取得Form內容
			Elements inputData = doc.select("input");
			// System.out.println("input : " + inputData.toString());
			for (Element input : inputData) {
				String n, v, t;
				System.out.print(t = input.attr("type"));
				System.out.print("\t" + (n = input.attr("name")));
				System.out.println(" : " + (v = input.attr("value")));
				if (t.equalsIgnoreCase("hidden")) {
					formData.put(n, v);
				}
			}

			Elements selects = doc.select("select");
			for (Element select : selects) {
				System.out.println("select: " + select.attr("name"));
			}

			Elements options = doc.select("option");
			// System.out.println("input : " + inputData.toString());
			for (Element option : options) {
				System.out.printf("%s : %s%n", option.attr("value"),
						option.text());
			}

			Map<String, String> cookies = res.cookies();

			// 請求WHD2A01
			Connection connection01 = Jsoup
					.connect("http://aomp.judicial.gov.tw/abbs/wkw/WHD2A01.jsp");
			// connection01
			// .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
			connection01.header("origin", "http://aomp.judicial.gov.tw");
			connection01
					.referrer("http://aomp.judicial.gov.tw/abbs/wkw/WHD2A00.jsp");
			connection01.cookies(cookies);
			connection01.data("nccharset", formData.get("nccharset"));
			connection01.data("court", "TPD");
			connection01.data("button", "查詢");
			Response res01 = connection01.method(Method.POST).execute();
			Document doc01 = res01.parse();
			// System.out.println(doc01);

			// 取得Form內容
			Elements inputData01 = doc01.select("input");
			// System.out.println("input : " + inputData.toString());
			for (Element input : inputData01) {
				String n, v, t;
				System.out.print(t = input.attr("type"));
				System.out.print("\t" + (n = input.attr("name")));
				System.out.println(" : " + (v = input.attr("value")));
				formData.put(n, v);
			}

			// 請求WHD2A02
			Connection connection02 = Jsoup
					.connect("http://aomp.judicial.gov.tw/abbs/wkw/WHD2A02.jsp");
			// connection02
			// .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
			connection02.header("origin", "http://aomp.judicial.gov.tw");
			connection02
					.referrer("http://aomp.judicial.gov.tw/abbs/wkw/WHD2A01.jsp");
			connection02.cookies(cookies);
			connection02.data("nccharset", formData.get("nccharset"));
			connection02.data("proptype", "C52");
			connection02.data("saletype", "1");
			connection02.data("court", "TPD");
			Response res02 = connection02.method(Method.POST).execute();
			Document doc02 = res02.parse();
			// System.out.println(doc02);
			Elements inputData02 = doc02.select("input");
			formData.clear();
			String keyName = null;
			for (Element input : inputData02) {
				String t = input.attr("type");
				System.out.println(t);
				if (t.equals("hidden")) {
					String n = input.attr("name"), v = input.attr("value");
					// System.out.print(n = input.attr("name"));
					// System.out.println(" : " + (v = input.attr("value")));
					formData.put(n, v);
					if (n.length() == 32) {
						keyName = n;
					}
				}
			}

			// Elements selectData02 = doc02.select("option");
			// for(Element o:selectData02){
			// System.out.println(o.attr("value"));
			// System.out.println(o.text());
			// }

			/*
			 * nccharset:03B8E5A7
			 * 8CDA7E00044644D49A56138132FEA6ED:3B8876D27BE82AB5E1F0E6FD37CC5E69
			 * hsimun:all ctmd:all sec:all saledate1:0950530 saledate2:1031231
			 * crmyy: crmid: crmno: dpt: minprice1: minprice2: saleno: area1:
			 * area2: registeno:(unable to decode value) checkyn:all emptyyn:all
			 * rrange:(unable to decode value) comm_yn: owner1: order:odcrm
			 * courtX:TPD proptypeX:C52 saletypeX:1 query_typeX:db
			 */

			// nccharset=03B533EC&
			// B52A190BF7EDBE25B204D2FC0CD6E130=A7443CD2FD036A331B0AE41C0AA5FB79&
			// hsimun=all&ctmd=all&sec=all&
			// saledate1=1030101&saledate2=1031231&crmyy=&crmid=&crmno=&dpt=&
			// minprice1=&minprice2=&saleno=&area1=&area2=&registeno=&checkyn=all&
			// emptyyn=all&rrange=不分&comm_yn:=&owner1=&order=odcrm&proptypeX=C52&
			// courtX=TPD&saletypeX=1&query_typeX=db

			String url = "http://aomp.judicial.gov.tw/abbs/wkw/WHD2A03.jsp";

			StringBuilder cookieValue = new StringBuilder();
			for (String name : cookies.keySet()) {
				if (cookieValue.length() > 0) {
					cookieValue.append("; ");
				}
				cookieValue.append(name).append("=" + cookies.get(name));
			}

			StringBuilder data = new StringBuilder();
			data.append("nccharset").append("=")
					.append(formData.get("nccharset")).append("&");
			data.append(keyName).append("=").append(formData.get(keyName))
					.append("&");
			data.append("hsimun").append("=")
					.append(URLEncoder.encode("all", "Big5")).append("&");
			data.append("ctmd").append("=")
					.append(URLEncoder.encode("all", "Big5")).append("&");
			data.append("sec").append("=")
					.append(URLEncoder.encode("all", "Big5")).append("&");
			data.append("saledate1").append("=").append("1030101").append("&");
			data.append("saledate2").append("=").append("1031231").append("&");
			data.append("crmyy").append("=").append("").append("&");
			data.append("crmid").append("=").append("").append("&");
			data.append("crmno").append("=").append("").append("&");
			data.append("dpt").append("=").append("").append("&");
			data.append("minprice1").append("=").append("").append("&");
			data.append("minprice2").append("=").append("").append("&");
			data.append("saleno").append("=").append("").append("&");
			data.append("area1").append("=").append("").append("&");
			data.append("area2").append("=").append("").append("&");
			data.append("registeno").append("=").append("").append("&");
			data.append("checkyn").append("=").append("all").append("&");
			data.append("emptyyn").append("=").append("all").append("&");
			data.append("rrange").append("=").append("不分").append("&");

			data.append("comm_yn:").append("=").append("").append("&");
			data.append("owner1").append("=").append("").append("&");
			data.append("order").append("=").append("odcrm").append("&");
			data.append("proptypeX").append("=").append("C52").append("&");
			data.append("courtX").append("=").append("TPD").append("&");
			data.append("saletypeX").append("=").append("1").append("&");
			data.append("query_typeX").append("=").append("db");

			//System.out.println(data);
			Document finalDoc = HttpService.doPost(url,
					data.toString(), cookieValue.toString(),
					"http://aomp.judicial.gov.tw/abbs/wkw/WHD2A02.jsp", "Big5");

			Element element = finalDoc.select("tbody").first();
			Element element2 = element.select("table").first();
			System.out.println("table: \n" + element2);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
